package com.douqu.game.core.entity.ext;

import com.bean.core.buffer.ByteBuffer;
import com.douqu.game.core.entity.Player;
import com.douqu.game.core.entity.ext.data.challenge.*;
import com.douqu.game.core.entity.world.WorldOfficialBean;
import com.douqu.game.core.factory.FunctionFactory;
import com.douqu.game.core.factory.WorldInfoFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.CoreUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * @author wangzhenfei
 *         2017-10-18 18:12
 *         挑战的信息{1.副本， 2.竞技场， 3.官阶站}
 */
public class ChallengeInfo extends ExtInfo {

    private OfficialRankData officialData;

    private ArenaData arenaData;

    private InstanceData instanceData;

    private HeroTempleData heroTempleData;
    /**
     * 远征信息
     */
    private ExpeditionData expeditionData;

    private CampBattleData campBattleData;

    private BossData bossData;

    private WorldMapTaskData worldMapTaskData;

//    /**
//     * 单人战场的数据
//     */
//    private WorldMapData worldMapData;

    public ChallengeInfo(Player player)
    {
        super(player);

        officialData = new OfficialRankData(player);
        arenaData = new ArenaData(player);
        instanceData = new InstanceData();
        heroTempleData = new HeroTempleData();
        expeditionData = new ExpeditionData(player);
        campBattleData = new CampBattleData(player);
        bossData = new BossData(player);
        worldMapTaskData = new WorldMapTaskData(player);

//        worldMapData = new WorldMapData();
//        worldMapData.setSingle(true);
    }

    @Override
    public void writeTo(ByteBuffer buffer)
    {
        officialData.writeTo(buffer);

        arenaData.writeTo(buffer);

        instanceData.writeTo(buffer);

        heroTempleData.writeTo(buffer);

        expeditionData.writeTo(buffer);

        campBattleData.writeTo(buffer);

        bossData.writeTo(buffer);

        worldMapTaskData.writeTo(buffer);

//        worldMapData.writeTo(buffer);

        buffer.writeBoolean(false);
    }

    @Override
    public void loadFrom(ByteBuffer buffer)
    {
        officialData.loadFrom(buffer);

        arenaData.loadFrom(buffer);

        instanceData.loadFrom(buffer);

        heroTempleData.loadFrom(buffer);

        expeditionData.loadFrom(buffer);

        campBattleData.loadFrom(buffer);

        bossData.loadFrom(buffer);

        worldMapTaskData.loadFrom(buffer);

        buffer.readBoolean();
    }

    @Override
    public void checkRedPointRemindAddGoods(SGCommonProto.E_GOODS_TYPE type, int id) {
        if(type != SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_ASSETS){ //官阶战和竞技场的兑换只会是消耗assets
            return;
        }
        WorldInfoFactory worldInfo = WorldInfoFactory.getInstance();
        WorldOfficialBean rankBean = worldInfo.getWorldOfficialData().getRankInfoByObjectIndex(player);
        int myRank = rankBean== null ? 15 : rankBean.getRankId();
        if(officialData.checkHasExchangeReward(myRank)){
            player.cacheRedPoint(SGCommonProto.E_RED_POINT_TYPE.RED_POINT_TYPE_OFFICIAL_RANK);
        }

        if(CoreUtils.isFunctionUnLock(FunctionFactory.UNLOCK_COMPETITIVE_SYSTEM, player)){
            if(arenaData.checkHasExchangeReward(worldInfo.getArenaRankInfo().getArenaRankByObjectIndex(player))){
                player.cacheRedPoint(SGCommonProto.E_RED_POINT_TYPE.RED_POINT_TYPE_ARENA);
            }
        }

    }

    @Override
    public List<SGCommonProto.E_RED_POINT_TYPE> checkRedPointRemindAllCondition(long currentTime)
    {
        WorldInfoFactory worldInfo = WorldInfoFactory.getInstance();

        WorldOfficialBean rankBean = worldInfo.getWorldOfficialData().getRankInfoByObjectIndex(player);
        int myRank = rankBean== null ? 15 : rankBean.getRankId();
        rankBean = worldInfo.getWorldOfficialData().getYesterdayRankInfoByObjectIndex(player);
        int yesRand = rankBean== null ? 15 : rankBean.getRankId();
        List<SGCommonProto.E_RED_POINT_TYPE> list = null;
        if(CoreUtils.isFunctionUnLock(FunctionFactory.UNLOCK_Military_SYSTEM, player)
        && officialData.hasRedPointRemind(myRank, yesRand, worldInfo.getStartServerTime())){
            if(list == null){
                list = new ArrayList<>();
            }
            list.add(SGCommonProto.E_RED_POINT_TYPE.RED_POINT_TYPE_OFFICIAL_RANK);
        }


        if(CoreUtils.isFunctionUnLock(FunctionFactory.UNLOCK_COMPETITIVE_SYSTEM, player) &&
                arenaData.hasRedPointRemind(worldInfo.getArenaRankInfo().getArenaRankByObjectIndex(player), worldInfo.getArenaRankInfo().getYesterdayArenaRankByObjectIndex(player.getObjectIndex()),
                        worldInfo.getStartServerTime())){
            if(list == null){
                list = new ArrayList<>();
            }
            list.add(SGCommonProto.E_RED_POINT_TYPE.RED_POINT_TYPE_ARENA);
        }

        if(CoreUtils.isFunctionUnLock(FunctionFactory.UNLOCK_REPLICA_SYSTEM, player) &&
                instanceData.hasRedPointRemind()){
            if(list == null){
                list = new ArrayList<>();
            }
            list.add(SGCommonProto.E_RED_POINT_TYPE.RED_POINT_TYPE_INSTANCE);
        }

        if(CoreUtils.isFunctionUnLock(FunctionFactory.UNLOCK_HEROIC_SANCTUARY, player) &&
                heroTempleData.hasRedPointRemind()){
            if(list == null){
                list = new ArrayList<>();
            }
            list.add(SGCommonProto.E_RED_POINT_TYPE.RED_POINT_TYPE_HERO_TEMPLE);
        }

        if(CoreUtils.isFunctionUnLock(FunctionFactory.UNLOCK_EXPEDITION_SYSTEM, player) &&
                expeditionData.hasRedPointRemind()){
            if(list == null){
                list = new ArrayList<>();
            }
            list.add(SGCommonProto.E_RED_POINT_TYPE.RED_POINT_TYPE_EXPEDITION);
        }

        if(CoreUtils.isFunctionUnLock(FunctionFactory.WORLD_BOSS, player) &&
                WorldInfoFactory.getInstance().getWorldBossData().isBossActive() &&
                bossData.getRemainChallengeTimes() > 0){
            if(list == null){
                list = new ArrayList<>();
            }
            list.add(SGCommonProto.E_RED_POINT_TYPE.RED_POINT_TYPE_WORLD_BOSS);
        }

        if (CoreUtils.isFunctionUnLock(FunctionFactory.INTO_WORLD_MAP, player)
                && worldMapTaskData.checkRedPointRemindAllCondition()){
            if(list == null){
                list = new ArrayList<>();
            }
            list.add(SGCommonProto.E_RED_POINT_TYPE.RED_POINT_TYPE_WORLDMAP_TASK);
        }

        return list;
    }



    @Override
    public void checkInit()
    {
        officialData.checkInit();
        arenaData.checkInit();
        instanceData.checkInit();
        heroTempleData.checkInit();
        expeditionData.checkInit();
        campBattleData.checkInit();
        bossData.checkInit();
        worldMapTaskData.checkInit();
    }

    @Override
    public void init()
    {
        officialData.init();
        arenaData.init();
        instanceData.init();
        heroTempleData.init();
        expeditionData.init();
        campBattleData.init();
        bossData.init();
        worldMapTaskData.init();
    }

    @Override
    public void reset()
    {
        arenaData.reset();
        officialData.reset();
        instanceData.reset();
        heroTempleData.reset();
        expeditionData.reset();
        campBattleData.reset();
        bossData.reset();
        worldMapTaskData.reset();
    }
    @Override
    public void checkReset()
    {
        arenaData.checkReset();
        officialData.checkReset();
        instanceData.checkReset();
        heroTempleData.checkReset();
        expeditionData.checkReset();
        campBattleData.checkReset();
        bossData.checkReset();
        worldMapTaskData.checkReset();
    }



    public OfficialRankData getOfficialData() {
        return officialData;
    }

    public ArenaData getArenaData() {
        return arenaData;
    }

    public InstanceData getInstanceData() {
        return instanceData;
    }

    public HeroTempleData getHeroTempleData() {
        return heroTempleData;
    }

    public ExpeditionData getExpeditionData() {
        return expeditionData;
    }

    public CampBattleData getCampBattleData() {
        return campBattleData;
    }

    public BossData getBossData() {
        return bossData;
    }

    public WorldMapTaskData getWorldMapTaskData() {
        return worldMapTaskData;
    }

    public void setWorldMapTaskData(WorldMapTaskData worldMapTaskData) {
        this.worldMapTaskData = worldMapTaskData;
    }

//    public WorldMapData getWorldMapData() {
//        return worldMapData;
//    }
//
//    public void setWorldMapData(WorldMapData worldMapData) {
//        this.worldMapData = worldMapData;
//    }
}
